package com.prawn.ift_collequipment.service;

import java.util.*;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Selection;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import util.IdWorker;

import com.prawn.ift_collequipment.dao.EquipmentStatusDao;
import com.prawn.ift_collequipment.pojo.EquipmentStatus;

/**
 * 服务层
 * 
 * @author Administrator
 *
 */
@Service
public class EquipmentStatusService {

	@Autowired
	private EquipmentStatusDao equipmentStatusDao;
	
	@Autowired
	private IdWorker idWorker;

	/**
	 * 查询全部列表
	 * @return
	 */
	public List<EquipmentStatus> findAll() {
		return equipmentStatusDao.findAll();
	}

	
	/**
	 * 条件查询+分页
	 * @param whereMap
	 * @param page
	 * @param size
	 * @return
	 */
	public Page<EquipmentStatus> findSearch(Map whereMap, int page, int size) {
		Specification<EquipmentStatus> specification = createSpecification(whereMap);
		PageRequest pageRequest =  PageRequest.of(page-1, size);
		return equipmentStatusDao.findAll(specification, pageRequest);
	}

	
	/**
	 * 条件查询
	 * @param whereMap
	 * @return
	 */
	public List<EquipmentStatus> findSearch(Map whereMap) {
		Specification<EquipmentStatus> specification = createSpecification(whereMap);
		return equipmentStatusDao.findAll(specification);
	}

	/**
	 * 根据ID查询实体
	 * @param id
	 * @return
	 */
	@Cacheable(value = "equipmentStatus",key = "#id")
	public EquipmentStatus findById(String id) {
		return equipmentStatusDao.findById(id).get();
	}

	/**
	 * 增加
	 * @param equipmentStatus
	 */
	public void add(EquipmentStatus equipmentStatus) {
		equipmentStatus.setId( idWorker.nextId()+"" );
		equipmentStatusDao.save(equipmentStatus);
	}

	/**
	 * 修改
	 * @param equipmentStatus
	 */
	@CacheEvict(value = "equipmentStatus",key = "equipmentStatus.id")
	public void update(EquipmentStatus equipmentStatus) {
		equipmentStatusDao.save(equipmentStatus);
	}

	/**
	 * 删除
	 * @param id
	 */
	@CacheEvict(value = "equipmentStatus",key = "#id")
	public void deleteById(String id) {
		equipmentStatusDao.deleteById(id);
	}

	/**
	 * 动态条件构建
	 * @param searchMap
	 * @return
	 */
	private Specification<EquipmentStatus> createSpecification(Map searchMap) {

		return new Specification<EquipmentStatus>() {

			@Override
			public Predicate toPredicate(Root<EquipmentStatus> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> predicateList = new ArrayList<Predicate>();
                // 编号
                if (searchMap.get("id")!=null && !"".equals(searchMap.get("id"))) {
                	predicateList.add(cb.like(root.get("id").as(String.class), "%"+(String)searchMap.get("id")+"%"));
                }
                // 所属设备编号
                if (searchMap.get("equipmentId")!=null && !"".equals(searchMap.get("equipmentId"))) {
                	predicateList.add(cb.like(root.get("equipmentId").as(String.class), "%"+(String)searchMap.get("equipmentId")+"%"));
                }
                // 场景id
                if (searchMap.get("sceneId")!=null && !"".equals(searchMap.get("sceneId"))) {
                	predicateList.add(cb.like(root.get("sceneId").as(String.class), "%"+(String)searchMap.get("sceneId")+"%"));
                }
                // 运行状态
                if (searchMap.get("runningState")!=null && !"".equals(searchMap.get("runningState"))) {
                	predicateList.add(cb.like(root.get("runningState").as(String.class), "%"+(String)searchMap.get("runningState")+"%"));
                }
                // 时间状态
                if (searchMap.get("timeState")!=null && !"".equals(searchMap.get("timeState"))) {
                	predicateList.add(cb.like(root.get("timeState").as(String.class), "%"+(String)searchMap.get("timeState")+"%"));
                }
                // 心跳类型
                if (searchMap.get("heartbeanType")!=null && !"".equals(searchMap.get("heartbeanType"))) {
                	predicateList.add(cb.like(root.get("heartbeanType").as(String.class), "%"+(String)searchMap.get("heartbeanType")+"%"));
                }
                // IP
                if (searchMap.get("remoteAddress")!=null && !"".equals(searchMap.get("remoteAddress"))) {
                	predicateList.add(cb.like(root.get("remoteAddress").as(String.class), "%"+(String)searchMap.get("remoteAddress")+"%"));
                }
                // 连接方式
                if (searchMap.get("linkType")!=null && !"".equals(searchMap.get("linkType"))) {
                	predicateList.add(cb.like(root.get("linkType").as(String.class), "%"+(String)searchMap.get("linkType")+"%"));
                }
                // 信号强度
                if (searchMap.get("signalIntensity")!=null && !"".equals(searchMap.get("signalIntensity"))) {
                	predicateList.add(cb.like(root.get("signalIntensity").as(String.class), "%"+(String)searchMap.get("signalIntensity")+"%"));
                }
                // 上报周期,单位s
                if (searchMap.get("reportingCycle")!=null && !"".equals(searchMap.get("reportingCycle"))) {
                	predicateList.add(cb.like(root.get("reportingCycle").as(String.class), "%"+(String)searchMap.get("reportingCycle")+"%"));
                }
                // 采集周期,单位s
                if (searchMap.get("acquisitionCycle")!=null && !"".equals(searchMap.get("acquisitionCycle"))) {
                	predicateList.add(cb.like(root.get("acquisitionCycle").as(String.class), "%"+(String)searchMap.get("acquisitionCycle")+"%"));
                }
                // 存储周期,单位s
                if (searchMap.get("storageCycle")!=null && !"".equals(searchMap.get("storageCycle"))) {
                	predicateList.add(cb.like(root.get("storageCycle").as(String.class), "%"+(String)searchMap.get("storageCycle")+"%"));
                }
                // 创建者
                if (searchMap.get("createBy")!=null && !"".equals(searchMap.get("createBy"))) {
                	predicateList.add(cb.like(root.get("createBy").as(String.class), "%"+(String)searchMap.get("createBy")+"%"));
                }
                // 更新者
                if (searchMap.get("updateBy")!=null && !"".equals(searchMap.get("updateBy"))) {
                	predicateList.add(cb.like(root.get("updateBy").as(String.class), "%"+(String)searchMap.get("updateBy")+"%"));
                }
                // 备注信息
                if (searchMap.get("remarks")!=null && !"".equals(searchMap.get("remarks"))) {
                	predicateList.add(cb.like(root.get("remarks").as(String.class), "%"+(String)searchMap.get("remarks")+"%"));
                }
                // 删除标记
                if (searchMap.get("delFlag")!=null && !"".equals(searchMap.get("delFlag"))) {
                	predicateList.add(cb.like(root.get("delFlag").as(String.class), "%"+(String)searchMap.get("delFlag")+"%"));
                }
				
				return cb.and( predicateList.toArray(new Predicate[predicateList.size()]));

			}
		};

	}

}
